package com.example.webrestfulcrud.component;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 我的国际化解析器
 */
public class MyLocaleResolver implements LocaleResolver {


    @Override
    public Locale resolveLocale(HttpServletRequest request) {

        String language = request.getParameter("language");

        if (!StringUtils.isEmpty(language)) {
            /**
             * 因为传进来的是 zh_CN en_US
             * 所以可以用_进行分割
             */
            String[] s = language.split("_");

            Locale locale = new Locale(s[0],s[1]);
            return locale;
        }
        Locale defaultLocale = Locale.getDefault();
        return defaultLocale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
